package com.kang.controller;

import com.kang.commons.Result;
import com.kang.service.SseEmitterServer;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * @program: service
 * @description: 通知控制类
 * @author: K.faWu
 * @create: 2022-04-11 16:11
 **/
@RestController
@RequestMapping("/msg")
public class NotifyController {
    /**
     * 用于创建连接（将用户注册到server中）
     */
    @GetMapping("/connect/{userId}")
    public SseEmitter connect(@PathVariable String userId) {
        return SseEmitterServer.connect(userId);
    }

    @GetMapping("/push/{message}")
    public ResponseEntity<String> push(@PathVariable(name = "message") String message) {
        SseEmitterServer.batchSendMessage(message);
        return ResponseEntity.ok("送消息给所有人");
    }

    @GetMapping("/test/{userId}")
    public Result<?> send(@PathVariable String userId){
        SseEmitterServer.sendMessage(userId, "测试推送消息");
        return Result.success();
    }
}
